package com.example.myspringboot.vagueik;

import org.springframework.util.StringUtils;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZhengShan
 * @Date 2024 07 01 14 53
 **/
public class IkAnalyzerSupport {

    /**
     * IK分词
     * @param target
     * @return
     */
    public static List<String> iKSegmenterToList(String target) throws Exception{
        if(StringUtils.isEmpty(target)){
            return new ArrayList();
        }
        List<String> result = new ArrayList<>();
        StringReader sr= new StringReader(target);
        // false:关闭智能分词 (对分词的精度影响较大)
        IKSegmenter ik = new IKSegmenter(sr,true);
        Lexeme lex;
        while((lex=ik.next())!=null){
            String lexemeText= lex.getLexemeText();
            result.add(lexemeText);
        }
        return result;
    }

}
